package com.ccx.coachsite.downloadmgr;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import android.content.Context;

import com.ccx.coachsite.data.SystemConst;
import com.ccx.coachsite.util.FileUtil;

public class DownloadDAO {
	
	private final static String HISTOTY_FILE="download_histoty.cache";
	
	/**
	 * 添加历史记录
	 * @param item
	 */
	public static void addHistory(DownloadItem item)
	{
		try {
			HashMap<String, DownloadItem> inMap=getDownloadHistoryMap();
			inMap=inMap==null?new HashMap<String, DownloadItem>():inMap;
			String key=item.getAvailVersion().replaceAll("\\.", "_")+item.getPkgName();
			inMap.put(key, item);
			writeMapToFile(inMap);
		} catch (Exception e) {
		}
	}
	
	/**
	 * 删除某历史记录
	 * @param item
	 */
	public static void removeHistory(DownloadItem item)
	{
		try {
			HashMap<String, DownloadItem> inMap=getDownloadHistoryMap();
			if(inMap==null)
				return;
			String key=item.getAvailVersion().replaceAll("\\.", "_")+item.getPkgName();
			inMap.remove(key);
			writeMapToFile(inMap);
		} catch (Exception e) {
		}
	}
	
	/**
	 * 删除所有历史记录
	 */
	public static void removeAll()
	{
		String filePath=SystemConst.DOWNLOAD_DIR+HISTOTY_FILE;
		if(FileUtil.isFileExits(filePath))
			new File(filePath).delete();
	}
	
	/**
	 * 是否下载过
	 * @param item
	 * @return
	 */
	public static boolean isExistHistory(DownloadItem item)
	{
		try {
			HashMap<String, DownloadItem> inMap=getDownloadHistoryMap();
			if(inMap==null)
				return false;
			String key=item.getAvailVersion().replaceAll("\\.", "_")+item.getPkgName();
			return inMap.containsKey(key);
		} catch (Exception e) {
		}
		
		return false;
			
	}
	
	/**
	 * 获取下载历史记录集合
	 * @param ctx
	 * @return
	 */
	public static List<DownloadItem> getHistoryList(Context ctx)
	{
		try {
			HashMap<String, DownloadItem> inMap=getDownloadHistoryMap();
			if(inMap==null)
				return null;
			Iterator<String> keyIter=inMap.keySet().iterator();
			List<DownloadItem> dList=new ArrayList<DownloadItem>();
			while(keyIter.hasNext())
			{
				String key=keyIter.next();
				DownloadItem item=inMap.get(key);
				if(FileUtil.isFileExits(item.getFilePath()))
				{
					int status=DownloadService.getDownloadStatus(ctx, item);
					item.setStatus(status);
					dList.add(item);
				}
			}
			return dList;
		} catch (Exception e) {
		}
		
		return null;
	}//end getHistoryList
	
	/**
	 * 获取下载历史记录的MAP
	 * @return
	 */
	public static HashMap<String , DownloadItem> getDownloadHistoryMap()
	{
		ObjectInputStream objIS=null;
		String filePath=SystemConst.DOWNLOAD_DIR+HISTOTY_FILE;
		if(!FileUtil.isFileExits(filePath))//文件不存在
			return null;
		try {
			objIS=new ObjectInputStream(new FileInputStream(filePath));
			Object obj=objIS.readObject();
			HashMap<String, DownloadItem> inMap=(HashMap<String, DownloadItem>)obj;
			return inMap;
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			if(objIS!=null)
				try {
					objIS.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
		
		return null;
	}//end getUnNotifyMap
	
	/**
	 * 将Map对象写入到文件中去
	 * @param map
	 */
	private static void writeMapToFile(HashMap<String, DownloadItem> map)
	{
		ObjectOutputStream objOS=null;
		String filePath=SystemConst.DOWNLOAD_DIR+HISTOTY_FILE;
		File file=new File(filePath);
		try {
			objOS=new ObjectOutputStream(new FileOutputStream(file));
			objOS.writeObject(map);
		} catch (Exception e) {
		} finally{
			if(objOS!=null)
				try {
					objOS.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}// end try
		
	}//end writeMapToFile

}
